package com.example.mcdonaldapplication.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.mcdonaldapplication.R
import com.example.mcdonaldapplication.databinding.ActivityGoodsDetailBinding
import java.io.IOException

class GoodsDetailActivity : AppCompatActivity() {
    private lateinit var binding: ActivityGoodsDetailBinding

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityGoodsDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)
        supportActionBar?.hide()

        val name = intent.getStringExtra("name")
        val content = intent.getStringExtra("content")
        val price = intent.getStringExtra("price")
        val img = intent.getStringExtra("img")

        binding.tvTitle.text = name    //标题
        binding.tvGname.text = name    //名称
        binding.tvContent.text = content    //内容
        binding.tvUnitPrice.text = "单价:¥${price}"   //单价
        binding.tvTotalPrice2.text = "合计:¥${price}"    //合计
        //图片
        if (img == "0"){
            binding.imgBackground.setImageResource(R.drawable.hamburger0)
        } else if (img == "1"){
            binding.imgBackground.setImageResource(R.drawable.hamburger1)
        }

        initView()
    }

    @SuppressLint("SetTextI18n")
    private fun initView() {

        var totalNum = 1   //商品数量
        val price = intent.getStringExtra("price")!!  //单价
        var totalPrice: String //合计
        val from = intent.getStringExtra("from")!!
        //点击+增加单件商品数量,改变合计价格
        binding.btnInc.setOnClickListener {
            totalNum++
            binding.tvNum.text="$totalNum"
            totalPrice = (price.toInt() * totalNum).toString()
            binding.tvTotalPrice2.text = "合计:¥${totalPrice}"
        }

        //点击-减少单件商品数量，改变合计价格
        binding.btnDec.setOnClickListener {
            totalNum--
            if (totalNum <= 1){
                totalNum = 1
            }
            binding.tvNum.text="$totalNum"
            totalPrice = (price.toInt() * totalNum).toString()
            binding.tvTotalPrice2.text = "合计:¥${totalPrice}"
        }

        //点击加入购物车跳回到点餐页面
        val img = intent.getStringExtra("img")
        val name = intent.getStringExtra("name")
        val gid = intent.getStringExtra("gid")
        binding.tvAddtoCart.setOnClickListener {
            if(from == "order"){
                val intent = Intent(this,OrderActivity::class.java)
                addCart(gid.toString(), name.toString(),totalNum,price,img.toString())
                finish()
                startActivity(intent)
            }else{
                val intent = Intent(this,TakeawayActivity::class.java)
                addCart(gid.toString(), name.toString(),totalNum,price,img.toString())
                finish()
                startActivity(intent)
            }
        }
    }
    private fun addCart(gid:String, name:String, num:Int, price:String, img:String) {
        try {
            val prefs = getSharedPreferences("cart", Context.MODE_PRIVATE)
            val outputNum = prefs.getInt("${name}num", 0)
            val editor = getSharedPreferences("cart", Context.MODE_PRIVATE).edit()
            editor.putString(name, name)
            if(outputNum > 0) {
                editor.putInt("${name}num", outputNum + num)
            }else{
                editor.putInt("${name}num", num)
            }
            editor.putString("${name}price", price)
            editor.putString("${name}img", img)
            editor.putString("${name}gid",gid)
            editor.apply()
        }
        catch (e: IOException){
            e.printStackTrace()
        }
    }
}